package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.entity.Room;
import com.xz.courseschedule.mapper.RoomMapper;
import com.xz.courseschedule.service.RoomService;
import com.xz.courseschedule.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: zys
 * @CreateTime: 2025-05-24
 */

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Override
    public Result getList() {
        List<Room> list = super.list();
        return Result.success("成功返回教室列表", list);
    }

    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error(400, "教室ID不能为空");
        }
        Room room = super.getById(id);
        if (room == null) {
            return Result.error(404, "教室不存在");
        }
        return Result.success("成功获取教室信息", room);
    }

    @Override
    public Result add(Room room) {
        if (room == null) {
            return Result.error(400, "教室信息不能为空");
        }
        // 检查教室编号是否已存在
        if (room.getRoomNo() != null) {
            LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<Room>()
                    .eq(Room::getRoomNo, room.getRoomNo());
            Room existingRoom = super.getOne(wrapper);
            if (existingRoom != null) {
                return Result.error(400, "教室编号已存在");
            }
        }
        boolean result = super.save(room);
        if (result) {
            return Result.success("教室添加成功", room);
        } else {
            return Result.error(500, "教室添加失败");
        }
    }

    @Override
    public Result update(Room room) {
        if (room == null || room.getId() == null) {
            return Result.error(400, "教室信息或ID不能为空");
        }
        // 检查教室是否存在
        Room existingRoom = super.getById(room.getId());
        if (existingRoom == null) {
            return Result.error(404, "教室不存在");
        }
        // 检查教室编号是否与其他教室重复
        if (room.getRoomNo() != null) {
            LambdaQueryWrapper<Room> checkWrapper = new LambdaQueryWrapper<Room>()
                    .eq(Room::getRoomNo, room.getRoomNo())
                    .ne(Room::getId, room.getId());
            Room duplicateRoom = super.getOne(checkWrapper);
            if (duplicateRoom != null) {
                return Result.error(400, "教室编号已存在");
            }
        }
        boolean result = super.updateById(room);
        if (result) {
            return Result.success("教室更新成功", room);
        } else {
            return Result.error(500, "教室更新失败");
        }
    }

    @Override
    public Result delete(Long id) {
        if (id == null) {
            return Result.error(400, "教室ID不能为空");
        }
        // 物理删除
        boolean result = super.removeById(id);
        if (result) {
            return Result.success("教室删除成功");
        } else {
            return Result.error(500, "教室删除失败");
        }
    }

    @Override
    public Result deleteBatch(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error(400, "教室ID列表不能为空");
        }
        // 物理删除
        boolean result = super.removeByIds(Arrays.asList(ids));
        if (result) {
            return Result.success("批量删除教室成功");
        } else {
            return Result.error(500, "批量删除教室失败");
        }
    }
}
